<?php session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
//  Copyright (C) 2004-2006  Autodesk, Inc.
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of version 2.1 of the GNU Lesser
//  General Public License as published by the Free Software Foundation.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-->
<html>

<head>
<?php include ("config.php");
	  include ("utils.php");
	  include(ROOT_DIR_CLASES.'/cCliente.php');
	  include(ROOT_DIR_CLASES.'/point.php');
?>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <meta http-equiv="content-script-type" content="text/javascript">
    <meta http-equiv="content-style-type" content="text/css">
    <link href="<?php echo ESTILO?>/globalStyles.css" rel="stylesheet" type="text/css">
	<link href="<?php echo ESTILO?>/otherStyles.css" rel="stylesheet" type="text/css">
	<link href="<?php echo ESTILO?>/viewer.css" rel="stylesheet" type="text/css">
	<link href="<?php echo ESTILO?>/bykom.css" rel="stylesheet" type="text/css">

    
</head>

<body onLoad="OnPageLoad()"> 

	
<?php

require_once('../common.php');
require_once($webExtensionsDirectory . 'www/mapviewerphp/layerdefinitionfactory.php');	
require_once('layer_functions.php');
try
{
	//---------------------------------------------------//
		$_SESSION['countExcesos']++;
		$cuenta = $_REQUEST['cuentaSelect'];
		$filtroHistorico = $_SESSION['filtroHistorico'];
		$identificador = $_REQUEST['identificador'];
		$desc = $_REQUEST['excesoVelocidad'];
		$lonExceso = quitCero($_REQUEST['lonExceso']);
		$latExceso = quitCero($_REQUEST['latExceso']);
		$typeMark = $_REQUEST['typeMark'];		
		$dateExceso = str_ireplace(",", " ", $_REQUEST['dateExceso']);
		$velExceso = $_REQUEST['velExceso'];
 		switch ($typeMark){
		case 1 : $symbolIcon = 'Limite'; 
				 $descripcion = "Exceso de ". $desc . " Km (".$dateExceso ." - " . $velExceso . " Km)";
				 break;
		case 2 : $symbolIcon = 'Evento'; 
				 $descripcion = "Evento ". str_ireplace("%", " ", $velExceso) ." (". $dateExceso .")";
				 break;
		default : $symbolIcon = 'Limite'; 
				  $descripcion = "Exceso de ". $desc . " Km (".$dateExceso ." - " . $velExceso . " Km)";
				  break;
		}
 		
 		//---------------------------------------------------//   	    
   			// Initialize
    		MgInitializeWebTier($webconfigFilePath);
    		$sessionId = ($_SERVER['REQUEST_METHOD'] == "POST")?$_POST['SESSION']: $_GET['SESSION'];
    		$userInfo = new MgUserInformation($sessionId);
    		$siteConnection = new MgSiteConnection();
   	 		$siteConnection->Open($userInfo);
    		$resourceService = $siteConnection->CreateService(MgServiceType::ResourceService);
    		$featureService = $siteConnection->CreateService(MgServiceType::FeatureService);      
           
    		
    		//---------------------------------------------------//
    		// Open the map
    		$map = new MgMap();
    		$map->Open($resourceService, AVL_MAP);
            $name1 = "PointsEx".$_SESSION['countExcesos'];
			$classDefinition = getPointClassDefinition($name1);
			$featureSchema = new MgFeatureSchema("PointSchema", "Point schema");
			$featureSchema->GetClasses()->Add($classDefinition);             
			$featureSourceName = "Session:$sessionId//".$name1.".FeatureSource";
			$resourceIdentifier = new MgResourceIdentifier($featureSourceName);
			$wkt = "LOCALCS[\"*XY-MT*\",LOCAL_DATUM[\"*X-Y*\",10000],UNIT[\"Meter\", 1],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH]]";
			$sdfParams = new MgCreateSdfParams("ArbitraryXY", $wkt, $featureSchema);
    		$featureService->CreateFeatureSource($resourceIdentifier, $sdfParams);
			$batchPropertyCollection = new MgBatchPropertyCollection();
			$wktReaderWriter = new MgWktReaderWriter();
			$agfReaderWriter = new MgAgfReaderWriter();
			$geometryFactory = new MgGeometryFactory();

			//$propertyCollection = MakePointCar("aa", "", $descripcion, "a", "a", "a", $lonExceso, $latExceso, $dateExceso, "a", "a", "a", "a", "a");
			$propertyCollection = MakeSimplePoint($descripcion, $lonExceso, $latExceso);
			$batchPropertyCollection->Add($propertyCollection);
			unset($propertyCollection);	
			$cmd = new MgInsertFeatures($name1, $batchPropertyCollection); 
			$featureCommandCollection = new MgFeatureCommandCollection();
			$featureCommandCollection->Add($cmd);
	
			$featureService->UpdateFeatures($resourceIdentifier, $featureCommandCollection, false);    
    		$factory = new LayerDefinitionFactory();

    		// Create a mark symbol
    		$resourceId = AVL_SYMBOLS;
    		$symbolName = $symbolIcon;
    		$width = '16';  // unit = points
    		$height = '16'; // unit = points
    		$color = 'FF'.COLOR_POSI_SELECT;
    		$markSymbol = $factory->CreateMarkSymbol($resourceId, $symbolName, $width, $height, $color);   
    
    		// Create a text symbol
    		$text = "IDENTIFICADOR";
    		$fontHeight = FONTSIZE_LEGEND_MOVIL;
    		$foregroundColor = COLOR_LEGEND_MOVIL;
    		$textSymbol = $factory->CreateTextSymbol($text, $fontHeight, $foregroundColor);

    		// Create a point rule.
    		$legendLabel = 'trees';
    		$filter = '';
    		$pointRule = $factory->CreatePointRule($legendLabel, $filter, $textSymbol, $markSymbol);
      
    		// Create a point type style.
    		$pointTypeStyle = $factory->CreatepointTypeStyle($pointRule);
      
    		// Create a scale range.
    		$minScale = '0';
    		$maxScale = '1000000000000';
    		$pointScaleRange = $factory->CreateScaleRange($minScale, $maxScale, $pointTypeStyle);      

    		// Create the layer definiton.
    		$featureName = 'PointSchema:'.$name1;
    		$geometry = 'GEOM';
    		$layerDefinition = $factory->CreateLayerDefinition($featureSourceName, $featureName, $geometry, $pointScaleRange);
    		//---------------------------------------------------//
    		// Add the layer to the map
    		$newLayer = add_layer_definition_to_map($layerDefinition, $name1, $descripcion, $sessionId, $resourceService, $map, false);
    		add_layer_to_group($newLayer, "Historico ".$filtroHistorico." - ".$identificador, "Historico ".$filtroHistorico." - ".$identificador, $map);

    		$layerCollection = $map->GetLayers();
    
    		if ($layerCollection->Contains($name1))
    		{
        		$pointsLayer = $layerCollection->GetItem($name1);
        		$pointsLayer->SetVisible(true);
    		}
    
    		//---------------------------------------------------//
    		$sessionIdName = "Session:$sessionId//".AVL_MAP.".Map";
    		$sessionResourceID = new MgResourceIdentifier($sessionIdName);
			$sessionResourceID->Validate();
    		$map->Save($resourceService, $sessionResourceID);

}
catch (MgException $e)
{
	echo "<script language=\"javascript\" type=\"text/javascript\"> \n";
	echo "    alert(\" " . $e->GetMessage() . " \"); \n";
	echo "</script> \n";
}

///////////////////////////////////////////////////////////////////////////////////
?>
<script language="javascript" type="text/javascript">
        function OnPageLoad()
        {
        	parent.mapFrame.Refresh();
            //parent.mapFrame.ZoomToScale(0.5);
           	parent.mapFrame.ZoomToView(<?= $lonExceso ?>, <?= $latExceso ?>, <?= ZOOM_AVL ?>, true);
        }
    </script>
</body>
</html>


